package com.dgshanger.sanhxiaofeisc.mvp.model

import android.text.TextUtils
import com.blankj.utilcode.util.EncryptUtils
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.PayResultEntity
import com.dgshanger.sanhxiaofeisc.entity.VipDetailEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.VipCardContract
import com.google.gson.Gson
import com.tencent.mm.opensdk.modelpay.PayReq
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.RxHttpObserver

/**
 * VipCardModel
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/2 11:49.
 */

class VipCardModel : VipCardContract.Model {

    override fun getBuyAgreement(httpObserver: RxHttpObserver<String>?) {
        AppApi.Api().buyAgreement
                .compose(RxHelper.io_main())
                .subscribe(httpObserver)

    }

    override fun getPayReqString(wxContentBean: PayResultEntity.WxContentBean?): String {
        if (wxContentBean == null) {
            return ""
        }
        return Gson().toJson(PayReq()
                .apply {
                    appId = wxContentBean.appid
                    sign = wxContentBean.sign
                    timeStamp = wxContentBean.timestamp
                    packageValue = wxContentBean.packageX
                    prepayId = wxContentBean.prepayid
                    partnerId = wxContentBean.partnerid
                    nonceStr = wxContentBean.noncestr
                })
    }

    override fun vipBuy(vip_id: String?, buy_qty: String?, buy_type: String?, user_pay_pwd: String?, httpObserver: RxHttpObserver<PayResultEntity>?) {
        AppApi.Api()
                .vip_buy(vip_id,
                        buy_qty,
                        buy_type,
                        if (TextUtils.isEmpty(user_pay_pwd)) null else EncryptUtils.encryptMD5ToString(user_pay_pwd).toLowerCase())
                .compose(RxHelper.io_main())
                .subscribe(httpObserver)
    }

    override fun loadVipDetail(vip_id: String, httpObserver: RxHttpObserver<VipDetailEntity>) {
        AppApi.Api()
                .vip_detail(vip_id)
                .compose(RxHelper.io_main())
                .subscribe(httpObserver)
    }
}
